import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.core.TermCriteria;
import org.opencv.highgui.Highgui;
import org.opencv.ml.CvANN_MLP;
import org.opencv.ml.CvANN_MLP_TrainParams;

public class NN {

	Mat inputs;
	Mat outputs;
	CvANN_MLP_TrainParams params;
	CvANN_MLP MLP;
	int input_size = 0;

	NN() {
		System.loadLibrary("opencv_java244");
		// inputs.create(new Size(160*168,1),CvType.CV_8UC1);

		MLP = new CvANN_MLP();

	}

	public void setParam() {

		params = new CvANN_MLP_TrainParams();
		TermCriteria term = new TermCriteria(0, 100, 0);
		params.set_term_crit(term);
		params.set_train_method(CvANN_MLP_TrainParams.BACKPROP);
		params.set_bp_dw_scale(0.1);
		params.set_bp_moment_scale(0.1);
	}

	public void MLP() {

		Mat layerSizes = new Mat(new Size(3, 1), CvType.CV_32S);
		int[] data = new int[2];
		data[0] = input_size;
		layerSizes.put(0, 0, data);
		data[0] = 45;
		layerSizes.put(0, 1, data);
		data[0] = 2;
		layerSizes.put(0, 2, data);

		System.out.println("" + data.length);
		MLP.create(layerSizes, CvANN_MLP.SIGMOID_SYM, 0, 0);
		int i = MLP.train(inputs, outputs, new Mat(), new Mat(), params, 0);
		System.out.println("i = " + i);

	}

	public void createDataSet() {
		inputs = new Mat(new Size(307200, 60), CvType.CV_32F);
		outputs = new Mat(new Size(2, 60), CvType.CV_32F);

		Mat img = null;
		int i = 1;
		for (; i <=10; i++) {
			img = Highgui.imread(i+".jpg");
			int img_width = img.width();
			int img_height = img.height();
			int size = img.height() * img.width();
			input_size = size;
			System.out.println("size = " + size);
			double[] vec = new double[size];

			int mm = 0;
			for (int nr = 0; nr < img_height; nr++) {
				for (int nc = 0; nc < img_width; nc++) {
					vec[mm] = img.get(nr, nc)[0];
					mm = mm + 1;
				}
			}

			

			System.out.println("total inputs" + inputs.total());
			for (int j = 0; j < size; j++) {
				float[] a = new float[2];
				a[0] = (float) vec[i];
				int in = i - 1;
				inputs.put(in, i, a);
			}

			System.out.println("" + inputs.get(0, size - 1)[0]);
			if (i <= 23) {

				float[] a = new float[2];
				a[0] = (float) 1;
				outputs.put(0, 0, a);

				a[1] = (float) 0;

				outputs.put(0, 1, a);
			}

			else if (i >= 24) {
				float[] b = new float[2];
				b[0] = (float) 0;
				outputs.put(0, 0, b[0]);
				b[1] = (float) 1;
				outputs.put(0, 1, b[1]);
			}
		}
		
		System.out.println("total = "+i);
	}

	public void test() {

		Mat img = Highgui.imread("test.jpg");
		int img_width = img.width();
		int img_height = img.height();
		int size = img.height() * img.width();

		System.out.println("size = " + size);
		double[] vec = new double[size];

		int mm = 0;
		for (int nr = 0; nr < img_height; nr++) {
			for (int nc = 0; nc < img_width; nc++) {
				vec[mm] = img.get(nr, nc)[0];
				mm = mm + 1;
			}
		}

		System.out.println("" + vec[size - 1]);

		System.out.println("" + inputs.total());
		for (int i = 0; i < size; i++) {
			float[] a = new float[2];
			a[0] = (float) vec[i];
			inputs.put(0, i, a);
		}

		System.out.println("" + inputs.get(0, size - 1)[0]);

		Mat outputss = new Mat(new Size(2, 1), CvType.CV_32F);

		MLP.predict(inputs, outputss);
		System.out.println("output:----------------------------------");
		System.out.println("" + outputss.get(0, 0)[0]);
		System.out.println("" + outputss.get(0, 1)[0]);

	}

	public static void main(String[] argv) {

		NN neuralnetwork = new NN();
		neuralnetwork.createDataSet();
		neuralnetwork.setParam();
		neuralnetwork.MLP();
		neuralnetwork.test();

	}
}
